package com.yxb.cms.service;

import com.yxb.cms.architect.constant.BussinessCode;
import com.yxb.cms.architect.utils.BussinessMsgUtil;
import com.yxb.cms.architect.utils.DateUtil;
import com.yxb.cms.dao.CoursesMapper;
import com.yxb.cms.dao.StuCourseMapper;
import com.yxb.cms.dao.expmle.StuCourseExample;
import com.yxb.cms.domain.bo.BussinessMsg;
import com.yxb.cms.domain.vo.Courses;
import com.yxb.cms.domain.vo.StuCourse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.nutz.json.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 
 */

@Service
public class StuCourseService {
    private Logger log = LogManager.getLogger(StuCourseService.class);

    @Autowired
    private StuCourseMapper stuCourseMapper;

    @Autowired
    private CoursesMapper coursesMapper;


    @Transactional
    public BussinessMsg insertStudents(StuCourse stuCourse, String currUserName){

        try {
            StuCourseExample stuCourseExample = new StuCourseExample();
            stuCourseExample.createCriteria().andCourseidEqualTo(stuCourse.getCourseid());
            //查询限制人数
            Courses courses = coursesMapper.selectByPrimaryKey(stuCourse.getCourseid());
            long l = stuCourseMapper.countByExample(stuCourseExample);
            if(courses.getLimitsPeople()==l){
                return BussinessMsgUtil.returnCodeMessage(BussinessCode.EXCEEDING_THE_NUMBER_LIMIT);
            }
            stuCourseMapper.insertSelective(stuCourse);
        } catch (Exception e) {
            log.error("保存学生信息方法内部错误",e);
            throw e;
        }
        return BussinessMsgUtil.returnCodeMessage(BussinessCode.GLOBAL_SUCCESS);
    }

    /**
     * 更新学生
     * @param students 学生实体
     */
    @Transactional
    public BussinessMsg updateByPrimaryKeySelective(StuCourse stuCourse, String currUserName){
        try {
            StuCourseExample stuCourseExample = new StuCourseExample();
            stuCourseExample.createCriteria().andCourseidEqualTo(stuCourse.getCourseid());
            //查询限制人数
            Courses courses = coursesMapper.selectByPrimaryKey(stuCourse.getCourseid());
            long l = stuCourseMapper.countByExample(stuCourseExample);
            if(courses.getLimitsPeople()==l){
                return BussinessMsgUtil.returnCodeMessage(BussinessCode.EXCEEDING_THE_NUMBER_LIMIT);
            }
            stuCourseMapper.updateByPrimaryKeySelective(stuCourse);
        } catch (Exception e) {
            log.error("保存学生信息方法内部错误",e);
            throw e;
        }
        return BussinessMsgUtil.returnCodeMessage(BussinessCode.GLOBAL_SUCCESS);
    }



    /**
     * 删除学生
     * @param stuId 学生ID
     */
    @Transactional
    public BussinessMsg deleteByPrimaryKeySelective(Long stuCourseId){
        try {
            stuCourseMapper.deleteByPrimaryKey(stuCourseId);
        } catch (Exception e) {
            log.error("删除学生信息方法内部错误",e);
            throw e;
        }
        return BussinessMsgUtil.returnCodeMessage(BussinessCode.GLOBAL_SUCCESS);
    }


    public String selectStuCoursePageList(StuCourse stuCourse,String userName){
        if(userName=="超级管理员" || userName.equals("超级管理员")){
            System.out.print("TEST SUCCESS");
            userName=null;
        }
        List<StuCourse> stuCourses = stuCourseMapper.selectStuCoursePage(stuCourse,userName);

        Long count = stuCourseMapper.count(stuCourse);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data", stuCourses);
        return Json.toJson(map);
    }


    //获取学生编号
    private String generateSno(){
        long time = new Date().getTime();
        String s = String.valueOf(time);
        String sno = s.substring(s.length() - 5, s.length() - 1);
        return DateUtil.getSysYear()+sno;
    }

}